package cn.funeralobjects.dictms.service.impl;

import cn.funeralobjects.common.jpa.service.*;
import cn.funeralobjects.common.repository.*;
import cn.funeralobjects.common.service.annotataion.CommonService;
import cn.funeralobjects.common.service.exception.DataConflictException;
import cn.funeralobjects.dictms.BasicInfo;
import cn.funeralobjects.dictms.entity.EDict;
import cn.funeralobjects.dictms.entity.EDictRelate;
import cn.funeralobjects.dictms.model.DictRelate;
import cn.funeralobjects.dictms.repository.EDictRelateRepository;
import cn.funeralobjects.dictms.service.DictRelateService;
import cn.funeralobjects.dictms.service.DictService;
import cn.funeralobjects.util.Assert;
import cn.funeralobjects.util.NumberUtils;
import org.springframework.data.repository.CrudRepository;

import javax.annotation.Resource;

/**
 * @author FuneralObjects
 * Create date: 2020/6/3 1:33 AM
 */
@CommonService
public class DictRelateServiceImpl implements DictRelateService,
        JpaModifyService<DictRelate, EDictRelate, Integer>,
        JpaEntityService<EDictRelate, Integer>,
        JpaCodeService<EDictRelate, Integer>,
        JpaEnableService<EDictRelate, Integer>,
        JpaDelSaltService<EDictRelate, Integer>,
        JpaCommitService<Integer> {

    @Resource
    private EDictRelateRepository eDictRelateRepository;

    @Resource
    private DictService dictService;

    @Override
    public void checkConflict(DictRelate dictRelate) throws DataConflictException {
        this.mustNotConflictByCode(dictRelate.getCode());
    }

    @Override
    public EDictRelate toEntity(DictRelate dictRelate) {
        if (dictRelate == null) {
            return null;
        }
        EDictRelate entity = new EDictRelate();
        EDict headDict = new EDict();

        headDict.setId(dictRelate.getHeadDictId());
        entity.setCode(dictRelate.getCode())
                .setDepth(dictRelate.getDepth())
                .setHeadDict(headDict)
                .setRemark(dictRelate.getRemark())
                .setRule(dictRelate.getRule());
        return entity;
    }

    @Override
    public CodeRepository<EDictRelate, Integer> codeRepository() {
        return eDictRelateRepository;
    }

    @Override
    public CommitRepository<Integer> commitRepository() {
        return eDictRelateRepository;
    }

    @Override
    public DelSaltRepository<EDictRelate, Integer> delSaltRepository() {
        return eDictRelateRepository;
    }

    @Override
    public EnableRepository<EDictRelate, Integer> enableRepository() {
        return eDictRelateRepository;
    }

    @Override
    public EntityRepository<EDictRelate, Integer> entityRepository() {
        return eDictRelateRepository;
    }

    @Override
    public CrudRepository<EDictRelate, Integer> modifyRepository() {
        return eDictRelateRepository;
    }

    @Override
    public void checkConflict(DictRelate dictRelate, Integer withoutId) throws DataConflictException {
        this.mustNotConflictByCode(dictRelate.getCode(), withoutId);
    }

    @Override
    public void copyToEntity(DictRelate sourceModify, EDictRelate targetEntity) {
        targetEntity.setRule(sourceModify.getRule())
                .setRemark(sourceModify.getRemark())
                .setDepth(sourceModify.getDepth())
                .setCode(sourceModify.getCode());
    }

    @Override
    public boolean isValid(Integer id) {
        return NumberUtils.notZero(id);
    }

    @Override
    public void validateModify(DictRelate dictRelate) {
        Assert.argAssert(dictRelate, "dictRelate");
        dictService.mustExistsById(dictRelate.getHeadDictId());
    }

    @Override
    public String getName() {
        return ENTITY_NAME;
    }

    @Override
    public String getModuleName() {
        return BasicInfo.MODULE_NAME;
    }

    @Override
    public Integer getExistsDelSalt() {
        return (Integer) eDictRelateRepository.getDefaultExistsDelSalt();
    }
}
